/*
 *
 * Created on January 8, 2008, 11:07 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sqs.delegates;

/**
 *
 * @author Lalitha
 */

import sqs.ejb.sessionbean.QuestionnaireMgr;
import sqs.model.MCQQuestion;
import sqs.model.MultipleAnsMCQQuestion;
import sqs.model.NumericQuestion;
import sqs.model.Option;
import sqs.model.Questionnaire;
import sqs.model.Question;
import javax.naming.Context;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import sqs.model.RatingQuestion;
import sqs.model.SingleAnsMCQQuestion;

public class ReportMgrDelegate {
    private QuestionnaireMgr facade;
    
    /** Creates a new instance of QuestionnaireMgrDelegate */
    public ReportMgrDelegate() {
        // temporary disabled, the JEE still not working
        //facade = this.lookupBean();
    }
    
    /** Looks-up the session bean */
    private QuestionnaireMgr lookupBean() {
        try {
            Context c = new InitialContext();
            return (QuestionnaireMgr) c.lookup("java:comp/env/ejb/QuestionnaireMgr");
            
        } catch(NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
            throw new RuntimeException(ne);
        }
    }
    
    /**
     * Returns all the Questionnaire objects
     * Parameter: activeFlag indicates to take only active questionnaire
     **/
    /*
    public List<Questionnaire> getAllQuestionnaires(boolean activeFlag) {
        ArrayList<Questionnaire> data = null;
        ArrayList<Questionnaire> qnaireList = new ArrayList<Questionnaire>();
        try {
            data = (ArrayList)facade.getAllQuestionnaires();
            for(Questionnaire qnaire : data) {
                if (activeFlag) {
                    if(qnaire.getActive().equals(Questionnaire.ACTIVE))
                        qnaireList.add(qnaire);
                } else {
                    qnaireList.add(qnaire);
                }
            }
        } catch(Exception re) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,re);
        }
        return qnaireList;
    } 
    */
    
    public List<Questionnaire> getAllQuestionnaires(boolean activeFlag) {
        List<Questionnaire> result = new ArrayList<Questionnaire>();
        
        // Temporary populate my own data
        Questionnaire qnaire1 = new Questionnaire();
        qnaire1.setQuestionnaireId(1);
        qnaire1.setActive(Questionnaire.ACTIVE);
        qnaire1.setQuestionnaireTitle("Wellness");
        qnaire1.setDescription("Questionnaire about your health and life style");
        result.add(qnaire1);
        
        Questionnaire qnaire2 = new Questionnaire();
        qnaire2.setQuestionnaireId(2);
        qnaire2.setActive(Questionnaire.ACTIVE);
        qnaire2.setQuestionnaireTitle("Course");
        qnaire2.setDescription("Questionnaire about J2EE course taught in ISS");
        result.add(qnaire2);
        
        Questionnaire qnaire3 = new Questionnaire();
        qnaire3.setQuestionnaireId(3);
        qnaire3.setActive(Questionnaire.ACTIVE);
        qnaire3.setQuestionnaireTitle("Transport");
        qnaire3.setDescription("Questionnaire about public transport in Singapore");
        result.add(qnaire3);
        
        Questionnaire qnaire4 = new Questionnaire();
        qnaire4.setQuestionnaireId(4);
        qnaire4.setActive(Questionnaire.ACTIVE);
        qnaire4.setQuestionnaireTitle("Investment");
        qnaire4.setDescription("Questionnaire about your investment plan");
        result.add(qnaire4);
        
        return result;
    }
    
    /**
     * Returns a Questionnaire object specified by the Questionnaire ID
     **/
    /* Temporary disabled, pending for Edmund to complete the EJB
    public Questionnaire getQuestionnaire(int qnaireId) {
        Questionnaire qnaire = null;
        try {
            qnaire = (Questionnaire)facade.getQuestionnaireByQuestionnaireId(qnaireId);
        } catch(Exception re) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,re);
        }
        return qnaire;
    }
    */
    
    public Questionnaire getQuestionnaire(int qnaireId) {
        Questionnaire qnaire = null;
        Question qn = null;
        Option opt = null;
        List<Option> optList;
        List<Question> qnList = new ArrayList<Question>();
        
        switch(qnaireId) {
            case 1:
                // Create 1st Questionnaire
                qnaire = new Questionnaire();
                qnaire.setQuestionnaireId(1);
                qnaire.setQuestionnaireTitle("Wellness");
                qnaire.setDescription("Questionnaire about your health and life style");

                // Create Questions & Options
                qn = new SingleAnsMCQQuestion();
                qn.setQuestionId(1);
                qn.setQuestionText("What is your gender?");
                qn.setMandatory(Question.MANDATORY);
                qn.setLayout(Question.HORIZONTAL_LAYOUT);
                
                optList = new ArrayList<Option>();
                opt = new Option();
                opt.setOptionId(1);
                opt.setOptionText("Female");
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(2);
                opt.setOptionText("Male");
                optList.add(opt);
                qn.setOptions(optList);
                qnList.add(qn);

                qn = new RatingQuestion();
                qn.setQuestionId(2);
                qn.setQuestionText("How old are you?");
                qn.setLayout(Question.VERTICAL_LAYOUT);
                
                optList = new ArrayList<Option>();
                opt = new Option();
                opt.setOptionId(1);
                opt.setOptionText("Less than 20 yrs old");
                opt.setRating(10);
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(2);
                opt.setOptionText("Between 20 to 29 yrs old");
                opt.setRating(20);
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(3);
                opt.setOptionText("Between 30 to 39 yrs old");
                opt.setRating(30);
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(4);
                opt.setOptionText("Between 40 to 49 yrs old");
                opt.setRating(40);
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(5);
                opt.setOptionText("Between 50 to 59 yrs old");
                opt.setRating(50);
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(6);
                opt.setOptionText("60 yrs old and above");
                opt.setRating(60);
                optList.add(opt);
                qn.setOptions(optList);
                qnList.add(qn);
                
                // Create Questions & Options
                qn = new NumericQuestion();
                qn.setQuestionId(3);
                qn.setQuestionText("What is your height (cm)?");
                qnList.add(qn);

                // Create Questions & Options
                qn = new NumericQuestion();
                qn.setQuestionId(4);
                qn.setQuestionText("What is your weight (kg)?");
                qnList.add(qn);

                // Create Questions & Options
                qn = new RatingQuestion();
                qn.setQuestionId(5);
                qn.setQuestionText("How often do you exercise?");
                qn.setLayout(Question.VERTICAL_LAYOUT);
                
                optList = new ArrayList<Option>();
                opt = new Option();
                opt.setOptionId(1);
                opt.setOptionText("0 - 1 days per week");
                opt.setRating((float)0.5);
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(2);
                opt.setOptionText("2 - 3 days per week");
                opt.setRating((float)2.5);
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(3);
                opt.setOptionText("4 - 6 days per week");
                opt.setRating(5);
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(4);
                opt.setOptionText("everyday");
                opt.setRating(7);
                optList.add(opt);
                qn.setOptions(optList);
                qnList.add(qn);
                
                // Create Questions & Options
                qn = new MultipleAnsMCQQuestion();
                qn.setQuestionId(6);
                qn.setQuestionText("Which fast food do you normally patronize?");
                qn.setLayout(Question.VERTICAL_LAYOUT);
                
                optList = new ArrayList<Option>();
                opt = new Option();
                opt.setOptionId(1);
                opt.setOptionText("Mc Donnald");
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(2);
                opt.setOptionText("Burger King");
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(3);
                opt.setOptionText("Kentucky Friend Chicken");
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(4);
                opt.setOptionText("Others");
                optList.add(opt);
                qn.setOptions(optList);
                qnList.add(qn);
                qnaire.setQuestions(qnList);

                break;

            case 3:
                // Create 2nd Questionnaire
                qnaire = new Questionnaire();
                qnaire.setQuestionnaireId(3);
                qnaire.setQuestionnaireTitle("Transport");
                qnaire.setDescription("Questionnaire about public transport in Singapore");

                // Create Questions & Options
                qn = new MultipleAnsMCQQuestion();
                qn.setQuestionId(1);
                qn.setQuestionText("Which public transport do you normally take?");
                qn.setMandatory(Question.MANDATORY);
                qn.setLayout(Question.HORIZONTAL_LAYOUT);
                
                optList = new ArrayList<Option>();
                opt = new Option();
                opt.setOptionId(1);
                opt.setOptionText("Bus");
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(2);
                opt.setOptionText("MRT");
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(3);
                opt.setOptionText("Taxi");
                optList.add(opt);
                opt = new Option(); 
                opt.setOptionId(4);
                opt.setOptionText("Private Cars");
                optList.add(opt);
                qn.setOptions(optList);
                qnList.add(qn);
                qnaire.setQuestions(qnList);

                break;
        }
        
        return qnaire;
    }

}
